package com.meida.student.app.scholartyrant.handler;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.mybatis.interceptor.GetInterceptor;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.module.bus.client.entity.BusScholartyrant;
import com.meida.module.bus.provider.service.BusOrderAppraiseService;
import com.meida.module.bus.provider.service.BusOrderService;
import com.meida.module.system.client.entity.SysDictData;
import com.meida.module.user.client.entity.AppEducational;
import com.meida.module.user.client.entity.AppSchool;
import com.meida.module.user.client.entity.AppUser;
import com.meida.module.user.client.entity.AppWorkhistory;
import com.meida.module.user.provider.service.AppEducationalService;
import com.meida.module.user.provider.service.AppWorkhistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.List;

/**
 * 学霸主页
 */
@Component
public class ScholartyrantDetailHandler implements GetInterceptor {

    @Autowired
    private BusOrderService busOrderService;

    @Autowired
    private BusOrderAppraiseService busOrderAppraiseService;

    @Autowired
    private AppEducationalService appEducationalService;

    @Autowired
    private AppWorkhistoryService appWorkhistoryService;


    @Override
    public void prepare(CriteriaQuery cq, EntityMap entityMap) {
        Long userId = entityMap.getLong("userId");
        cq.clear();
        cq.select(BusScholartyrant.class, "subjectInfo", "selfIntroduction", "tags");
        cq.select(AppUser.class, "userName", "nickName", "avatar", "userNo", "personProfile");
        cq.eq(BusScholartyrant.class, "scholartyrantId", userId);
        cq.createJoin(AppUser.class).setMainField("scholartyrantId").setJoinField("userId");
    }

    @Override
    public void complete(CriteriaQuery cq, EntityMap map) {
        Long answerUserId = cq.getLong("userId");
        //课业辅导数量(已完成接单总数量)
        Long finshOrderNum = busOrderService.countFinshOrderNum(answerUserId);
        map.put("finshOrderNum", finshOrderNum);
        //平均得分(评价我分值平均分)
        EntityMap totalScoreInfo = busOrderAppraiseService.getTotalScoreInfo(answerUserId);
        BigDecimal totalNum = totalScoreInfo.getBigDecimal("totalNum");
        BigDecimal totalScore = totalScoreInfo.getBigDecimal("totalScore");
        BigDecimal averageScore = BigDecimal.ZERO;
        if (FlymeUtils.gtzero(totalNum)) {
            averageScore = totalScore.divide(totalNum,2, BigDecimal.ROUND_HALF_UP);
        }
        map.put("averageScore", averageScore);
        //用户评价(评价我用户总人数)
        Integer appraiseNum = busOrderAppraiseService.countAppraiseNum(answerUserId);
        map.put("appraiseNum", appraiseNum);
        //二次订阅率(再次下单用户数量/总下单用户总数) 下单次数大于1人数/所有下单人数
        Integer allUserOrderNum = busOrderService.countAllUserOrderNum(answerUserId);
        Integer againUserOrderNum = busOrderService.countAgainUserOrderNum(answerUserId);
        map.put("againOrderProportion", "0%");
        if (allUserOrderNum > 0) {
            NumberFormat numberFormat = NumberFormat.getInstance();
            // 设置精确到小数点后2位
            numberFormat.setMaximumFractionDigits(2);
            String result = numberFormat.format((float) againUserOrderNum / (float) allUserOrderNum * 100);
            map.put("againOrderProportion", result + "%");
        }
        //教育背景
        List<EntityMap> educationalList = appEducationalService.listByUserId(answerUserId, criteriaQuery -> {
            criteriaQuery.select(AppEducational.class, "major", "educationDate");
            criteriaQuery.select(AppSchool.class, "schoolName", "schoolLogo");
            criteriaQuery.select(SysDictData.class, "dicDataTitle educationName");
            criteriaQuery.createJoin(AppSchool.class).setMainField("schoolId").setJoinField("schoolId");
            criteriaQuery.createJoin(SysDictData.class).setMainField("educationId").setJoinField("dictDataId");
        });
        map.put("educationalList", educationalList);
        List<EntityMap> workHistoryList = appWorkhistoryService.listByUserId(answerUserId, criteriaQuery -> {
            criteriaQuery.select(AppWorkhistory.class, "workhistoryId", "companyName", "positionName", "companyAddress", "workingDate");
        });
        map.put("workHistoryList", workHistoryList);
    }
}
